#!/usr/bin/python
import os
import sys
import time
import re
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

## Some setup
filePort = 438
ServerVersion = "0.Alpha"

## Splash
def splash():
    os.system("cls")
    print(" ")
    print("   /$$$$$$              /$$           /$$$$$$                     ")
    print("  /$$__  $$            | $$          /$$__  $$                    ")
    print(" | $$  \__/  /$$$$$$  /$$$$$$       | $$  \ $$  /$$$$$$   /$$$$$$ ")
    print(" | $$ /$$$$ /$$__  $$|_  $$_//$$$$$$| $$$$$$$$ /$$__  $$ /$$__  $$")
    print(" | $$|_  $$| $$$$$$$$  | $$ |______/| $$__  $$| $$  \ $$| $$  \ $$")
    print(" | $$  \ $$| $$_____/  | $$ /$$     | $$  | $$| $$  | $$| $$  | $$")
    print(" |  $$$$$$/|  $$$$$$$  |  $$$$/     | $$  | $$| $$$$$$$/| $$$$$$$/")
    print("  \______/  \_______/   \___/       |__/  |__/| $$____/ | $$____/ ")
    print("                                              | $$      | $$      ")
    print("                                              | $$      | $$      ")
    print("                                              |__/      |__/      ")
    print("      _________                                 ")
    print("     /   _____/ ______________  __ ___________  ")
    print("     \_____  \_/ __ \_  __ \  \/ // __ \_  __ \ ")
    print("     /        \  ___/|  | \/\   /\  ___/|  | \/ ")
    print("    /_______  /\___  >__|    \_/  \___  >__|    ")
    print("            \/     \/                 \/        ")
    print(" ")
    print("                                            -by Jo")
    print(" ")
    print("                     " + ServerVersion)
    print(" ")
    print(" ")
    print(" ")

def handleAliases():
	print("\t[alias] Building aliases")
	for i in os.walk(fileDir):
		ignore = fileDir.replace("\\","/") + "/.hg"
		search = i[0].replace("\\","/")
		pattern = re.findall(ignore,search)
		try:
			pattern[0]
		except:
			ignore = fileDir.replace("\\","/") + "/Aliases"
			search = i[0].replace("\\","/")
			pattern = re.findall(ignore,search)
			try:
				pattern[0]
			except:
				rootDir = i[0].replace(fileDir,"")
				rootDir = rootDir.replace("\\","/")
				rootDir = rootDir.lstrip("/")
				aliasFile = fileDir.replace("\\","/") + "/" + rootDir + "/alias"
				aliasPointer = fileDir + "/Aliases/"
				if os.path.exists(aliasFile):
					aliasNames = open(aliasFile,'r').read()
					alias = aliasNames.split("\n")
					for a in alias:
						open(aliasPointer + a,'w').writelines(rootDir)
		
## Gets Configuration
cwd = os.getcwd()
scriptdir = sys.argv[0]
scriptdir = os.path.dirname(scriptdir)
paths = scriptdir + "/Get-App.config"
file = open(paths, 'r')
path = [line.split(',') for line in file.readlines()]
config = []
for i in path:
	path = str(i[0])
	path = path.replace("\n","")
	config.append(path)
for i in config:
	con = i.split("=")
	key = ""
	fileDir = ""
	if con[0] == "key":
		key = con[1]
	if con[0] == "directory":
		fileDir = con[1]

## Serves the files
HandlerClass = SimpleHTTPRequestHandler
ServerClass  = BaseHTTPServer.HTTPServer
Protocol     = "HTTP/1.0"
if sys.argv[1:]:
    port = int(sys.argv[1])
else:
    port = filePort
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
splash()
#print("\tServing Installers on " + sa[0] + " port " + sa[1] + "...")
os.chdir(fileDir)
httpd.serve_forever()
os.chdir(scriptdir)

handleAliases()
#input()